package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.Strategy;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTheme;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("strategyDetail")
public class StrategyDetailController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyService strategyService;

    @Reference
    private IStrategyThemeService strategyThemeService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyTagService strategyTagService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        //pageInfo
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategyDetail/list";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id) {
        // strategies
        model.addAttribute("strategies", strategyService.list());
        // themes
        model.addAttribute("themes", strategyThemeService.list());
        if (id != null) {
            // strategyDetail
            StrategyDetail strategyDetail = strategyDetailService.get(id);
            strategyDetail.setStrategyContent(strategyDetailService.getContentById(strategyDetail.getId()));
            //tags
            String tags = strategyTagService.queryByStrategyDetailId(id);
            model.addAttribute("tags", tags);
            model.addAttribute("strategyDetail", strategyDetail);
        }
        return "strategyDetail/input";
    }

    @RequestMapping("/getCatalogByStrategyId")
    @ResponseBody
    public List<StrategyCatalog> getCatalogByStrategyId(Long strategyId) {
        try {
            return strategyCatalogService.queryCatalogByStrategyId(strategyId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(StrategyDetail detail, String tags) {
        try {
            strategyDetailService.saveOrUpdate(detail, tags);
            return AjaxResult.SUCCESS;
        } catch (LogicException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "网络异常请稍后重试!!!");

        }
    }
}
